1.4 Control Statements in C

Module 1.4 • Execution Mappings, Branching & Loop Structures

1.4.1 Introduction

By default, a C program executes statements one after another from top to bottom. However, many real-world problems require decisions, repetition, or skipping certain instructions.

Control statements allow us to change the normal flow of execution according to program requirements.

Using control statements, we can:

Control statements are mainly divided into three categories:

1.4.2 Decision Making Statements

Decision-making statements allow a program to choose different actions based on conditions.

A. if Statement

The if statement executes a block of code only when the specified condition is true.

Syntax

if(condition)
{
    statements;
}

Example

#include <stdio.h>
 
int main()
{
    int marks = 78;
 
    if(marks >= 50)
    {
        printf("Pass");
    }
 
    return 0;
}

Output

Pass

B. if...else Statement

The if...else statement provides two possible paths.

Syntax

if(condition)
{
    statements;
}
else
{
    statements;
}

Example

#include <stdio.h>
 
int main()
{
    int age = 16;
 
    if(age >= 18)
    {
        printf("Eligible to Vote");
    }
    else
    {
        printf("Not Eligible to Vote");
    }
 
    return 0;
}

Output

Not Eligible to Vote

C. else-if Ladder

Used when multiple conditions need to be checked.

Example

#include <stdio.h>
 
int main()
{
    int score = 82;
 
    if(score >= 90)
        printf("Grade A");
    else if(score >= 75)
        printf("Grade B");
    else if(score >= 60)
        printf("Grade C");
    else
        printf("Grade D");
 
    return 0;
}

Output

Grade B

Important Note

The moment one condition becomes true, the remaining conditions are skipped.

D. Nested if Statement

An if statement can be placed inside another if statement.

Example

#include <stdio.h>
 
int main()
{
    int age = 24;
    int salary = 35000;
 
    if(age >= 18)
    {
        if(salary >= 30000)
            printf("Loan Approved");
    }
 
    return 0;
}

Output

Loan Approved

E. switch Statement

The switch statement is useful when a variable can have many possible values.

Syntax

switch(expression)
{
    case value1:
        statements;
        break;
 
    case value2:
        statements;
        break;
 
    default:
        statements;
}

Example

#include <stdio.h>
 
int main()
{
    int day = 4;
 
    switch(day)
    {
        case 1:
            printf("Monday");
            break;
 
        case 2:
            printf("Tuesday");
            break;
 
        case 3:
            printf("Wednesday");
            break;
 
        case 4:
            printf("Thursday");
            break;
 
        default:
            printf("Invalid Day");
    }
 
    return 0;
}

Output

Thursday

1.4.3 Looping Statements

Loops are used when a set of statements must be executed repeatedly. C provides three loops:

A. while Loop

The condition is checked before executing the loop body.

Syntax

while(condition)
{
    statements;
}

Example

#include <stdio.h>
 
int main()
{
    int n = 1;
 
    while(n <= 5)
    {
        printf("%d ", n);
        n++;
    }
 
    return 0;
}

Output

1 2 3 4 5

B. do...while Loop

The loop body executes first and the condition is checked afterward.

Syntax

do
{
    statements;
}
while(condition);

Example

#include <stdio.h>
 
int main()
{
    int n = 1;
 
    do
    {
        printf("%d ", n);
        n++;
    }
    while(n <= 5);
 
    return 0;
}

Output

1 2 3 4 5

Important Feature

A do...while loop always executes at least one time.

C. for Loop

Used when the number of repetitions is known.

Syntax

for(initialization; condition; update)
{
    statements;
}

Example

#include <stdio.h>
 
int main()
{
    int i;
 
    for(i = 1; i <= 5; i++)
    {
        printf("%d ", i);
    }
 
    return 0;
}

Output

1 2 3 4 5

1.4.4 Nested Loops

A loop inside another loop is called a nested loop.

Example

#include <stdio.h>
 
int main()
{
    int i, j;
 
    for(i = 1; i <= 3; i++)
    {
        for(j = 1; j <= 4; j++)
        {
            printf("* ");
        }
 
        printf("\n");
    }
 
    return 0;
}

Output

* * * * * * * * * * * *

1.4.5 Infinite Loops

A loop that never stops executing is called an infinite loop.

Example

while(1)
{
    printf("Running...");
}

Since the condition is always true, the loop continues forever.

Common Causes

Example:

while(value = 1)
{
}

This becomes an infinite loop because value = 1 always produces a true value.

1.4.6 break Statement

The break statement immediately terminates a loop or switch block.

Example

#include <stdio.h>
 
int main()
{
    int i;
 
    for(i = 1; i <= 10; i++)
    {
        if(i == 6)
            break;
 
        printf("%d ", i);
    }
 
    return 0;
}

Output

1 2 3 4 5

1.4.7 continue Statement

The continue statement skips the remaining statements of the current iteration and moves to the next iteration.

Example

#include <stdio.h>
 
int main()
{
    int i;
 
    for(i = 1; i <= 5; i++)
    {
        if(i == 3)
            continue;
 
        printf("%d ", i);
    }
 
    return 0;
}

Output

1 2 4 5

1.4.8 goto Statement

The goto statement transfers program control to a labeled statement.

Syntax

goto label;
 
label:
statement;

Example

#include <stdio.h>
 
int main()
{
    int n = 1;
 
start:
 
    printf("%d ", n);
    n++;
 
    if(n <= 4)
        goto start;
 
    return 0;
}

Output

1 2 3 4

Note

Although valid, excessive use of goto can make programs difficult to understand and maintain.

1.4.9 Compound Statement (Block)

A group of statements enclosed within curly braces { } is called a block or compound statement.

Example

{
    int length = 10;
    int width = 5;
 
    printf("%d", length * width);
}

All statements inside the braces are treated as a single unit.

1.4.10 Comparison of Loops

Feature while do...while for
Condition CheckedBefore LoopAfter LoopBefore Loop
Executes At Least OnceNoYesNo
Best Used WhenIterations UnknownMust Run OnceIterations Known

1.4.11 Real-Life Uses of Control Statements

Decision Making

Loops

Jump Statements

Summary

Verify Comprehension: Technical Knowledge Assessment

Click your choice for each question to view feedback immediately. Complete all questions to evaluate your metric score.